<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_agent extends MY_Controller {

	function Admin_agent(){
		parent::MY_Controller();
		parent::check_admin_login();
        $this->load->model('Magent');
	}

	function agent_add(){
		$this->load->model('Mnetbar');
		$areas = $this->Mnetbar->get_area_array();
		$this->template->assign('areas',$areas);
		$this->template->view('admin/agent_add');
	}

    function agent_new(){
        $form = array('area_id','agent_no','agent_pwd','agent_name','agent_email');
        $data = $this->input->get_data($form);
        $this->load->model('Mnetbar');
        $data['area_name'] = $this->Mnetbar->get_area_name($data['area_id']);
        if($this->Magent->insert_new_agent($data)){
            echo '添加成功！&nbsp;<a href="javascript:window.history.back();">返回</a>&nbsp;|&nbsp;<a href="/admin_agent/agent_add/">继续添加</a>';
        } else {
            echo '添加失败！&nbsp;<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function agent_admin($offset=0){
        $this->load->model('Mnetbar');
		$areas = $this->Mnetbar->get_area_array();
		$this->template->assign('areas',$areas);
        
        $area_id = isset($_SESSION['area_id']) ? $_SESSION['area_id'] : '1';

        if($this->input->post('area_id')){
            $area_id = $this->input->post('area_id');
        }

        $this->template->assign('a_id',$area_id);

        $total = $this->Magent->get_agent_count($area_id);

		$this->load->library('pagination');

		$config['base_url'] = '/admin_agent/agent_admin/';
		$config['total_rows'] = $total;
		$config['per_page'] = '11';
		$config['uri_segment'] = 3;
		$config['num_links'] = 3;
		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();

		$limit = $config['per_page'];

        $agents = $this->Magent->get_agents($area_id,$offset,$limit);

        $this->template->view('admin/agent_admin',array('agents'=>$agents,'page_html'=>$page_html));

    }

    function agent_edit($agent_id='') {
        if(!$agent_id) die('no agent_id');
        $agent = $this->Magent->get_agent_info($agent_id);
        $this->template->assign('agent',$agent);
        $this->template->view('admin/agent_edit');
    }

    function edit() {
        $agent_id = $this->input->post('agent_id');
        if(!$agent_id) die('no agent_id');
        $form = array('agent_pwd','agent_name','agent_email');
        $data = $this->input->get_data($form);
        if($this->Magent->update_agent($agent_id,$data)){
            echo '修改成功！&nbsp;<a href="/admin_agent/agent_admin/">返回列表</a>&nbsp;|&nbsp;<a href="/admin_agent/agent_add/">添加新纪录</a>';
        } else {
            echo '修改失败！&nbsp;<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function agent_del($agent_id='') {
        if(!$agent_id) die('no agent_id');
        if($this->Magent->delete_agent($agent_id)){
            echo '删除成功！&nbsp;<a href="/admin_agent/agent_admin/">返回列表</a>&nbsp;|&nbsp;<a href="/admin_agent/agent_add/">添加新纪录</a>';
        } else {
            echo '删除失败！&nbsp;<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function bar_list($agent_no,$offset=0){
        $this->load->model('Mnetbar');
        
        $total = $this->Mnetbar->get_agent_bar_count($agent_no);

		$this->load->library('pagination');

		$config['base_url'] = '/admin_agent/bar_list/'.$agent_no;
		$config['total_rows'] = $total;
		$config['per_page'] = '11';
		$config['uri_segment'] = 4;
		$config['num_links'] = 3;
		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();

		$limit = $config['per_page'];

        $bars = $this->Mnetbar->get_agent_bars($agent_no,$offset,$limit);
        //print_r($bars);

        $this->template->view('admin/agent_bar_list',array('bars'=>$bars,'page_html'=>$page_html));
    }

    function order_list($agent_no,$offset=0){

        $agent = $this->Magent->get_agent_info_by_no($agent_no);
        
        $total = $this->Magent->get_agent_order_count($agent_no);

		$this->load->library('pagination');

		$config['base_url'] = '/admin_agent/order_list/'.$agent_no;
		$config['total_rows'] = $total;
		$config['per_page'] = '11';
		$config['uri_segment'] = 4;
		$config['num_links'] = 3;
		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();

		$limit = $config['per_page'];

        $orders = $this->Magent->get_agent_orders($agent_no,$offset,$limit);

        $this->template->view('admin/agent_order_list',array('orders'=>$orders,'page_html'=>$page_html,'agent'=>$agent));
        
    }

}

?>